package com.yimils.core.sys.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yimils.core.common.dto.GetQueryParams;
import com.yimils.core.sys.entity.UserRoleEntity;
import com.yimils.core.sys.service.UserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class PageUtils {

    @Resource(name = "userRoleService")
    private UserRoleService userRoleService;

    public void makePageParams(GetQueryParams getParams, Page pageParams) {

        if (getParams.getSuperiorId() != null) {
            QueryWrapper<UserRoleEntity> qw = new QueryWrapper<>();
            qw.lambda().eq(UserRoleEntity::getUserId, getParams.getSuperiorId());
            UserRoleEntity userRoleEntity = userRoleService.getOne(qw);
            if (userRoleEntity.getType().equals(UserRoleService.UR_SYS)) {
                getParams.setSuperiorId(null);
            }
        }

        if (StringUtils.isNotBlank(getParams.getOrderBy())
                && StringUtils.isNotBlank(getParams.getOrder())) {
            if (getParams.getOrder().equalsIgnoreCase("asc")) {
                pageParams.setAsc(getParams.getOrderBy());
            } else {
                pageParams.setDesc(getParams.getOrderBy());
            }
        }
    }
}
